// pages/detail/detail.js
var app = getApp()
const cai = wx.cloud.database().collection("cai")
const user = wx.cloud.database().collection("user")
const comment = wx.cloud.database().collection("comment")
var pinglun
var riqi
var second
var id,caiming
var dangqianpingfenshu, dangqianfenshu, peopleid, message = {},
  tempurl
var _options, nowid,nowstar
Page({

  /**
   * 页面的初始数据
   */
  data: {
    caicai: {},
    shitang: ["沙河一楼", "沙河二楼", "沙河三楼", "合一食堂", "学二食堂", "新北食堂", "其他"],
    comment: {},
    people: {},
    shoucang: 0,
    tujian: 0,
    test: 0,
    showModalStatus: false,
    showModalStatus1: false,
    animationData: {},
    star: -1,
    currentindex: 0,
    seedelete: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    pinglun = ''

    _options = options
    console.log(app.globalData.profile)
    id = options.id
    wx.cloud.callFunction({
      name: "getcomment",
      data: {
        id: id
      },
      success: (res) => {
        this.setData({
          comment: res.result,
          seedelete: false
        })
      }
    })
    cai.doc(id).get()
      .then(res => {
        caiming=res.data.name
        dangqianfenshu = res.data.star
        dangqianpingfenshu = res.data.pingfenshu
        console.log(res, this)
        this.setData({
          caicai: res.data

        })
        console.log(this.data.caicai)
        console.log("caicai", this.data.caicai._id)
        this.getzhuangtai()
      })

  },
  getzhuangtai() {
    user.where({
      user: app.globalData.openid
    }).get({
      success: (res) => {
        console.log("getzhuangtai", res)
        this.setData({
          shoucang: res.data[0].shoucang.includes(this.data.caicai._id),
          tujian: res.data[0].tujian.includes(this.data.caicai._id),
        })
        console.log("shoucang", this.data.shoucang)
        console.log("tujian", this.data.tujian)
      }
    })
  },
  changeshoucang() {
    if (app.globalData.openid == '') {
      wx.showToast({
        title: "登录后使用收藏",
        icon: "error"
      })
      return
    }
    let that = this
    console.log("点击收藏")
    if (!this.data.shoucang) {
      console.log("haimei收藏")
      user.where({
        user: app.globalData.openid
      }).update({
        data: {
          shoucang: wx.cloud.database().command.push(that.data.caicai._id)
        },
        success: (res) => {
          console.log("ok", res)
          console.log("shoucangzhuangtai", this.data.shoucang)
          this.setData({
            shoucang: true,
            test: 1
          })
          console.log(this)
          console.log(this.data.test, this.data.shoucang)
        },
        fail: (res) => {
          console.log("no", res)
        }
      })
    } else {
      console.log("!haimei收藏")
      user.where({
        user: app.globalData.openid
      }).update({
        data: {
          shoucang: wx.cloud.database().command.pull(that.data.caicai._id)
        },
        success: (res) => {
          console.log("quxiaoshoucangok", res)
          console.log("shoucangzhuangtai", this.data.shoucang)
          this.setData({
            shoucang: false,
            test: 0
          })
          console.log(this)
          console.log(this.data.test, this.data.shoucang)
        },
        fail: (res) => {
          console.log("no", res)
        }
      })
    }
  },
  changetujian() {
    if (app.globalData.openid == '') {
      wx.showToast({
        title: "登录后加入图鉴",
        icon: "error"
      })
      return
    }
    let that = this
    console.log("点击图鉴")
    if (!this.data.tujian) {
      console.log("haimei图鉴")
      user.where({
        user: app.globalData.openid
      }).update({
        data: {
          tujian: wx.cloud.database().command.push(that.data.caicai._id)
        },
        success: (res) => {
          console.log("ok", res)
          console.log("图鉴状态", this.data.tujian)
          this.setData({
            tujian: true,
            test: 1
          })
          console.log(this)
          console.log(this.data.test, this.data.tujian)
        },
        fail: (res) => {
          console.log("no", res)
        }
      })
    } else {
      console.log("已在图鉴里")
      user.where({
        user: app.globalData.openid
      }).update({
        data: {
          tujian: wx.cloud.database().command.pull(that.data.caicai._id)
        },
        success: (res) => {
          console.log("quxiao图鉴ok", res)
          console.log("图鉴zhuangtai", this.data.tujian)
          this.setData({
            tujian: false,
            test: 0
          })
          console.log(this)
          console.log(this.data.test, this.data.tujian)
        },
        fail: (res) => {
          console.log("no", res)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onLoad(_options)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  clickme: function () {
    if (app.globalData.openid == '') {
      wx.showToast({
        title: "登录后发表评价",
        icon: "error"
      })
      return
    }
    this.showModal();
  },

  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(700).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    this.setData({
      seedelete: false
    })
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(700).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false,
        showModalStatus1: false
      })
    }.bind(this), 200)
  },
  selectstar() {
    wx.showActionSheet({
      itemList: ['一星', '二星', "三星", "四星", "五星"],
      success: (res) => {
        this.setData({
          star: res.tapIndex + 1
        })
      }
    })
  },
  writecomment(res) {
    pinglun = res.detail.value
    console.log(pinglun)
  },
  submit() {
    this.hideModal()
    wx.showToast({
      title: '提交成功',
    })
    if (this.data.star == -1) {
      wx.showToast({
        title: '请选择星评',
        icon: "error"
      })
      return
    }
    let dt=new Date()
    console.log("profile", app.globalData.profile)
    comment.add({

      data: {
        cai: id,
        pinglun: pinglun,
        second: dt.valueOf(),
        //time: new Date().toLocaleDateString(),
        time:dt.getFullYear() + '/' + (dt.getMonth()+1) + '/' + dt.getDate(),
        openid: app.globalData.openid,
        star: this.data.star,
        nickname: app.globalData.profile.nickName,
        head: app.globalData.profile.avatarUrl,
        caiming:caiming
      }
    })
    console.log("dangqianfenshu",dangqianfenshu,"dangqianpingfenshu",dangqianpingfenshu)
    let newstar = (dangqianfenshu * dangqianpingfenshu + this.data.star) / (dangqianpingfenshu + 1)
    let newpinglunshu = dangqianpingfenshu + 1
    cai.doc(id).update({
      data: {
        star: newstar,
        pingfenshu: newpinglunshu
      },
      success: (res) => {
        console.log("ok", res)
        console.log(newstar, newpinglunshu, id)
      },
      fail: (res) => {
        console.log("fail", res)
      }
    })
    this.onLoad(_options)
  },
  gotodetail(res) {
    nowstar=this.data.comment[this.data.currentindex].star
    this.setData({
      currentindex: res.currentTarget.dataset.index
    })
    nowid = this.data.comment[this.data.currentindex].openid
    console.log(nowid)
    if (nowid == app.globalData.openid) {
      this.setData({
        seedelete: true
      })

    }
    this.showModal1();
  },
  showModal1: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(700).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus1: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  baocun() {
    console.log("用户要保存")
    cai.doc(id).get({
      success: (res) => {
        tempurl = res.data.image
        console.log(tempurl)
        wx.previewImage({
          urls: [tempurl],

        })
      }
    })


  },
  delete() {
    let that=this
    wx.showModal({
      title: '提示',
      content: '确定要删除评论吗',
      success(res) {
        if (res.confirm) {
          that.hideModal()
          console.log('用户点击确定')
          comment.doc(that.data.comment[that.data.currentindex]._id)
            .remove({})
          console.log(that.data.star)
          let newstar = (dangqianfenshu * dangqianpingfenshu - nowstar) / (dangqianpingfenshu - 1)
          let newpinglunshu = dangqianpingfenshu - 1
          cai.doc(id).update({
            data: {
              star: newstar,
              pingfenshu: newpinglunshu
            },
            success: (res) => {
              console.log("ok", res)
              console.log(newstar, newpinglunshu, id)
            },
            fail: (res) => {
              console.log("fail", res)
            }
          })

          that.onLoad(_options)
        } else if (res.cancel) {
          return
        }
      }
    })

  },
  
})